package org.dreamfly.netshop.actions.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.dreamfly.netshop.entity.Admin;
import org.dreamfly.netshop.entity.GoodsInfo;
import org.dreamfly.netshop.entity.Type;
import org.dreamfly.netshop.manage.AdminManager;
import org.dreamfly.netshop.manage.ClassManager;
import org.dreamfly.netshop.manage.GoodsInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.PropertyFilter.MatchType;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 用户管理Action. 使用Struts2 convention-plugin annotation定义Action参数.
 * 
 * @author calvin
 */
public class AdminAction extends ActionSupport {

    @Autowired
    private ClassManager classManager;
    @Autowired
    private GoodsInfoManager goodsInfoManager;

    private List<Type> allClass;

    @Autowired
    private AdminManager adminManager;

    private Admin admin;

    private Page<GoodsInfo> goodsInfo = new Page<GoodsInfo>(4);

    private Page<GoodsInfo> hotGoodsInfo = new Page<GoodsInfo>(4);

    private Page<GoodsInfo> spGoodsInfo = new Page<GoodsInfo>(4);

    public List<Type> getAllClass() {
        return allClass;
    }

    public Page<GoodsInfo> getGoodsInfo() {
        return goodsInfo;
    }

    public Page<GoodsInfo> getHotGoodsInfo() {
        return hotGoodsInfo;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public Page<GoodsInfo> getSpGoodsInfo() {
        return spGoodsInfo;
    }

    @Override
    public String execute() {
        // ActionContext ctx = ActionContext.getContext();
        // HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        // HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
        // classManager.delete((long) 21);
        allClass = classManager.getAll();
        goodsInfo.setOrder("DESC");
        goodsInfo.setOrderBy("addDate");
        goodsInfo = goodsInfoManager.getAll(goodsInfo);
        hotGoodsInfo.setOrder("DESC");
        hotGoodsInfo.setOrderBy("addDate");
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isHot", true, MatchType.EQUAL));
        hotGoodsInfo = goodsInfoManager.search(hotGoodsInfo, list);
        spGoodsInfo.setOrder("DESC");
        spGoodsInfo.setOrderBy("addDate");
        list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isrefinement", true, MatchType.EQUAL));
        spGoodsInfo = goodsInfoManager.getAll(spGoodsInfo);
        return SUCCESS;
    }

    public String checkLogin() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        // String codeO = (String) session.getAttribute("jpeg-random-code");
        // String codeI = request.getParameter("code");
        // if (!codeI.equals(codeO)) {
        // request
        // return NONE;
        // }

        if (admin == null || admin.getName() == null || admin.getPassword() == null || "".equals(admin.getName())
                || "".equals(admin.getPassword())) {
            try {
                ServletActionContext.getResponse().sendRedirect(request.getContextPath() + "/pages/adlogin.jsp");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return NONE;
        }
        admin = adminManager.CheckUserAndPwd(admin);
        if (admin == null) {
            try {
                ServletActionContext.getResponse().sendRedirect(request.getContextPath() + "/pages/adlogin.jsp");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return NONE;
        }
        session.setAttribute("member", admin);
        // request.setAttribute("needToPay", orderInfoManager.getAllNeedToPay(member.getId()).size());
        // request.setAttribute("needToShip", orderInfoManager.getNeedToShip(member.getId()).size());
        // request.setAttribute("needToSure", orderInfoManager.getNeedToSure(member.getId()).size());
        // request.setAttribute("success", orderInfoManager.getAllSuccess(member.getId()).size());
        return SUCCESS;
    }
}
